<?php

namespace Admin\Controller;

use User\Api\UserApi;
use Org\Util\Sync;
use Think\Model;

/**
 * Activity活动配置
 * @author Nico
 * 2016-07-11
 */
class ActivityController extends AdminController {

    /**
     * 活动列表
     * @author Nico
     * 2016-07-11
     */
    public function index() {
        $map['delflag']    =   array('EQ', 0);
        $list   =   $this->lists('activity', $map);
        int_to_string($list);

        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }

    public function add($name='',$bin='',$title='',$share='',$desc='',$link='',$imgUrl='',$successUrl='',$content='') {
        if (IS_POST) {
            $share_id = 0 ;
            if ($name == "" || $bin == '') {
                $this->error("模版名称或者简码不能为空");
            }else if ($share == "on") {
                $share_id = 1 ;
                // 上传文件
                $setting = array(
                    'mimes' => '', //允许上传的文件MiMe类型
                    'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                    'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
                );

                /* 调用文件上传组件上传文件 */
                /*             * 修改开始* */
                $pic_driver = C('PICTURE_UPLOAD_DRIVER');
                $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
                $info = $upload->upload();
                if (!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {// 上传成功
                    $imgUrl = $info["imgUrl"]["url"];
                }
                if($title == "" || $desc == "" || $link=='' || $imgUrl=='' || $successUrl==''){
                    $this->error("分享数据必填".$title.'/'.$desc.'/'.$link.'/'.$imgUrl.'/'.$successUrl);
                }
            }

            $data = array(
                'name' => $name,
                'bin' => $bin,
                'title' => $title,
                'share' => $share_id,
                'desc' => $desc,
                'link' => $link,
                'imgUrl' => $imgUrl,
                'successUrl' => $successUrl,
                'content' => $content,
                'createAt' => NOW_TIME,
                'updateAt' => NOW_TIME
            );
            $id = M('activity')->add($data);

            if (!$id) {
                $this->error("发布失败！");
            } else {
                $this->success('发布成功！', U('index'));
            }
        } else {
            $this->meta_title = '新增模版';
            $this->display();
        }
    }
    public function edit($id='',$name='',$bin='',$title='',$share='',$desc='',$link='',$imgUrl='',$successUrl='',$content='') {
        if (IS_POST) {
            $share_id = 0 ;
            if ($name == "" || $bin == '') {
                $this->error("模版名称或者简码不能为空");
            }else if ($share == "on") {
                $share_id = 1 ;

                if($title == "" || $desc == "" || $link=='' ||  $successUrl==''){
                    $this->error("分享数据必填".$title.'/'.$desc.'/'.$link.'/'.$imgUrl.'/'.$successUrl);
                }
            }
            // 上传文件
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();
            if ($info) {
                $data = array(
                    'name' => $name,
                    'bin' => $bin,
                    'title' => $title,
                    'share' => $share_id,
                    'desc' => $desc,
                    'link' => $link,
                    'imgUrl' => $info["imgUrl"]["url"],
                    'successUrl' => $successUrl,
                    'content' => $content,
                    'updateAt' => NOW_TIME
                );
            }else{
                $data = array(
                    'name' => $name,
                    'bin' => $bin,
                    'title' => $title,
                    'share' => $share_id,
                    'desc' => $desc,
                    'link' => $link,
                    'successUrl' => $successUrl,
                    'content' => $content,
                    'updateAt' => NOW_TIME
                );
            }

            M('activity')->where(array('id'=>$id))->save($data);

            $this->success('修改成功！', U('index'));
        } else {
            $result = M('activity')->where(array('id'=>$id))->find();
            $this->assign('data',$result);
            $this->meta_title = '编辑模版';
            $this->display();
        }
    }
    //删除商品
    public function del($id=""){
        IF ($id == "") {
            $this->error("刪除失败!");
        }
        $Data = array('status' => -1,'delflag'=>1,'updateAt'=>NOW_TIME);
        $Product = D('activity');
        if ($Product->where(array('id' => $id))->save($Data)) {
            $this->success("刪除成功!");
        } else {
            $this->error("刪除失败！");
        }
    }
}
